/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhu.profiteer.admin;

import java.io.IOException;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jhu.profiteer.data.Commodity;
import jhu.profiteer.data.CommodityJpaController;
import jhu.profiteer.data.exceptions.NonexistentEntityException;

/**
 *
 * @author Jeremy
 */
public class CommodityServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      String submit = request.getParameter("submit");

      if(null == submit)
      {
        this.getServletContext().getRequestDispatcher("/admin/commodities").forward(request, response);
        return;
      }

      HttpSession session = request.getSession();
      Vector<String> notices = (Vector<String>)session.getAttribute("notices");

      if(submit.equalsIgnoreCase("Create"))
      {
        boolean save = true;
        String url = "/admin/commodities";
        String name = request.getParameter("name");

        if((null == name) || name.isEmpty())
        {
          if(null == notices) notices = new Vector<String>();
          notices.add("You must supply a commodity name.");
          save = false;
          url = "/admin/commodity";
        }

        String description = request.getParameter("description");

        if((null == description) || description.isEmpty())
        {
          if(null == notices) notices = new Vector<String>();
          notices.add("You must supply a commodity description.");
          save = false;
          url = "/admin/commodity";
        }

        if(save)
        {
          Commodity c = new Commodity();
          c.setName(name);
          c.setDescription(description);
          CommodityJpaController jc = new CommodityJpaController();
          jc.create(c);
        }

        if(null != notices) session.setAttribute("notices", notices);
        this.getServletContext().getRequestDispatcher(url).forward(request, response);
      }
      else if(submit.equalsIgnoreCase("Update"))
      {
        boolean save = true;
        String idParam = request.getParameter("commodityId");
        String url = "/admin/commodities";

        if((null == idParam) || idParam.isEmpty())
        {
          if(null == notices) notices = new Vector<String>();
          notices.add("You must supply a commodity ID.");
          save = false;
        }

        String name = request.getParameter("name");

        if((null == name) || name.isEmpty())
        {
          if(null == notices) notices = new Vector<String>();
          notices.add("You must supply a commodity name.");
          save = false;
          url = "/admin/commodity?commodityId=" + idParam;
        }

        String description = request.getParameter("description");

        if((null == description) || description.isEmpty())
        {
          if(null == notices) notices = new Vector<String>();
          notices.add("You must supply a commodity description.");
          save = false;
          url = "/admin/commodity?commodityId=" + idParam;
        }

        if(save)
        {
          Commodity c = new Commodity();
          c.setId(Long.parseLong(idParam));
          c.setName(name);
          c.setDescription(description);
          CommodityJpaController jc = new CommodityJpaController();

          try
          {
            jc.edit(c);
          }
          catch(NonexistentEntityException ex)
          {
            if(null == notices) notices = new Vector<String>();
            notices.add("No commodity with that ID exists.");
          }
          catch(Exception ex)
          {
            if(null == notices) notices = new Vector<String>();
            notices.add("Error editing commodity.");
          }
        }

        if(null != notices) session.setAttribute("notices", notices);
        this.getServletContext().getRequestDispatcher(url).forward(request, response);
      }
      else if(submit.equalsIgnoreCase("Delete"))
      {
        CommodityJpaController jc = new CommodityJpaController();
        String param = request.getParameter("commodityId");
        Long id = 0L;

        if((null != param) && (!param.isEmpty()))
          id = Long.parseLong(param);
        else
        {
          if(null == notices) notices = new Vector<String>();
          notices.add("You must supply a commodity ID.");
        }

        try
        {
          jc.destroy(id);
          if(null == notices) notices = new Vector<String>();
          notices.add("You have successfully deleted the commodity");
        }
        catch (NonexistentEntityException ex)
        {
          if(null == notices) notices = new Vector<String>();
          notices.add("Invalid commodity ID!");
        }

        if(null != notices) session.setAttribute("notices", notices);
        this.getServletContext().getRequestDispatcher("/admin/commodities").forward(request, response);
      }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
